package com.calf.module.sms.job;

import com.calf.cn.service.BaseManager;
import com.calf.module.jpush.JPushUtil;
import com.calf.module.sms.entity.Sms;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 极光推送定时任务
 */
@Component(value = "pushJob")
public class ScheduledPushJob {

    @Autowired
    private BaseManager baseManager;

    public void execute() {
        System.out.println("==================================开始执行定时任务============================");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String alias = null;
        String content = null;
        String title = null;
        String url = null;
        String target = "1";

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime now = LocalDateTime.now();//
        LocalDateTime nowPlusOne = now.plusHours(1);

        String startTimestr = dateTimeFormatter.format(now);
        String endTimestr = dateTimeFormatter.format(nowPlusOne);

        String startTime = new StringBuilder(startTimestr).replace(14, 18, "00:00").toString();
        String endTime = new StringBuilder(endTimestr).replace(14, 18, "00:00").toString();

        Map<String,Object> parameters = new HashMap();
        parameters.put("startTime", startTime);//现在时间
        parameters.put("endTime", endTime);//往后一小时时间
        parameters.put("isTime",2);
        List<Sms> smsList = baseManager.query("Sms.queryByHour", parameters);

        if(CollectionUtils.isNotEmpty(smsList)){
            for(Sms sm:smsList){
                alias = sm.getAlias();
                content = sm.getSmsContent();
                title = sm.getSmsName();
                url = sm.getUrl();

                // 推送通知
                JPushUtil.jSend_notification(alias, content, title, url, target);
            }
        }


    }

    public static void main(String[] args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nowPlusOne = now.plusHours(1);

        String nowTimeStr = dateTimeFormatter.format(now);
        String plusOne = dateTimeFormatter.format(nowPlusOne);

        System.out.println(nowTimeStr+"=================="+plusOne);
    }

}
